package com.yanqu.road.server.cmd.rank;

import com.yanqu.road.entity.enums.*;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_RANK_WORSHIP, desc = "膜拜排行榜")
public class RankWorshipCmd extends AbstractCommand {

	private int rankType;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		RankProto.RankWorshipReqMsg reqMsg = RankProto.RankWorshipReqMsg.parseFrom(packet.getBytes());
		rankType = reqMsg.getRankType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(eRankType.EarnSpeed.getValue() != rankType && eRankType.PatronsAbility.getValue() != rankType &&
				eRankType.BeautyIntimacy.getValue() != rankType && eRankType.UnionEarnSpeed.getValue() != rankType &&
				eRankType.CrossEarnSpeed.getValue() != rankType && eRankType.CrossPatronsAbility.getValue() != rankType &&
				eRankType.CrossBeautyIntimacy.getValue() != rankType && eRankType.CrossUnionEarnSpeed.getValue() != rankType){
			ret = GameErrorCode.E_RANK_WORSHIP_TYPE;
			return;
		}
		if(eRankType.CrossEarnSpeed.getValue() == rankType || eRankType.CrossPatronsAbility.getValue() == rankType ||
				eRankType.CrossBeautyIntimacy.getValue() == rankType || eRankType.CrossUnionEarnSpeed.getValue() == rankType){
			// 跨服
			if(!ServerGroupMgr.isOpenCrossRank(GameServer.getInstance().getServerId())){
				ret = GameErrorCode.E_RANK_WORSHIP_CROSS_GROUP_RANK_NO_OPEN;
				return;
			}
			RankProto.CrossGroupWorshipReqMsg.Builder req = RankProto.CrossGroupWorshipReqMsg.newBuilder();
			req.setGroupId(ServerGroupMgr.getGroupIdByServerId(GameServer.getInstance().getServerId()));
			if (eRankType.CrossUnionEarnSpeed.getValue() == rankType) {
				req.setCorssType(rankType);
			} else {
				req.setCorssType(RankMgr.parseCrossRankType(rankType));
			}
			req.setGameType(rankType);
			req.setBigRankType(eBigRankType.Normal.getValue());
			if (eRankType.CrossUnionEarnSpeed.getValue() == rankType) {//发到商会跨服
				player.sendPacket(CrossProtocol.C_CROSS_UNION_GROUP_RANK_WORSHIP, req);
			} else {
				player.sendPacket(CrossProtocol.C_CROSS_GROUP_RANK_WORSHIP, req);
			}

		}else{
			// 本服
			RankProto.RankWorshipRespMsg.Builder respMsg = RankMgr.rankWorship(player,rankType);
			player.sendPacket(Protocol.U_RANK_WORSHIP, respMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			RankProto.RankWorshipRespMsg.Builder respMsg = RankProto.RankWorshipRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_RANK_WORSHIP, respMsg);
		}
	}

}
